/* Author: David McCoy dave.mccoy@cospandesign.com
 *
 *     This file is part of DLCM\SubServ.
 *
 *  DLCM\SubServ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DLCM\SubServ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DLCM\SubServ.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * GraphicsWidget.java
 *
 * Used to send images over LCM
 *
 * Created on Apr 2, 2010, 2:08:24 AM
 */

package com.cospandesign.gui.pubsub.widgets;

import pubsub.PubSubManager;
import dlcm.Member;
import dlcm.Structure;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;

public class GraphicsWidget extends javax.swing.JFrame implements Widget{

    String ChannelName;
    String MemberName;
    dlcm.Structure mStructure;
    dlcm.Member mMember;
    dlcm.Structure mVideoStructure;
    PubSubManager Parent;
    BufferStrategy mBufferStrategy;
    BufferedImage mBufferedImage;
    MemoryImageSource MIS;
    int [] RGB;

    private Color SelectedColor;

    /** Creates new form GraphicsWidget */
    public GraphicsWidget() {
        initComponents();
        SelectedColor = Color.RED;

        SetupCanvas();

    }

    // <editor-fold defaultstate="collapsed" desc="Static Functions">
    public static String GetWidgetName() {
        return "Graphics Widget";
    }
    // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Inherited Widget Functions">
    public void ReceiveMessage(Structure structure) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean CanUseMember(Member member) {
       boolean x = false;
       boolean y = false;
       boolean image_data = false;
        if (member.Data instanceof dlcm.Structure){
            dlcm.Structure videoStructure = (Structure) member.Data;

            for (Member m : videoStructure.getMemberList()){
                if (m.Name.matches("x")){
                    x = true;
                }
                else if (m.Name.matches("y")){
                    y = true;
                }
                else if (m.Name.matches("image_data")){
                    image_data = true;
                }
            }
        }
        return (x && y && image_data);
    }

    public void SetupWidget(String channelName, String memberName, Structure structure, PubSubManager parent) {
        ChannelName = channelName;
        MemberName = memberName;
        mStructure = structure;
        mMember = mStructure.getMember(memberName);
        Parent = parent;
        mVideoStructure = (Structure) mMember.Data;

        setTitle(memberName + "Medio Widget");
        setVisible(true);
    }

    public void setWidgetVisibility(boolean visible) {
        setVisible(visible);
    }
    // </editor-fold>

    public void SetupCanvas (){
        GraphicsCanvas.createBufferStrategy(2);
        mBufferStrategy = GraphicsCanvas.getBufferStrategy();
        RGB = new int [GraphicsCanvas.getWidth() * GraphicsCanvas.getHeight()];
        MIS = new MemoryImageSource(GraphicsCanvas.getWidth(), GraphicsCanvas.getHeight(), RGB, 0, GraphicsCanvas.getWidth());
        MIS.setAnimated(true);
        //mBufferedImage = new BufferedImage (GraphicsCanvas.getWidth(), GraphicsCanvas.getHeight(), BufferedImage.TYPE_INT_RGB);
        //mBufferedImage.setRGB(0, 0, GraphicsCanvas.getWidth(), GraphicsCanvas.getHeight(), RGB, 0, GraphicsCanvas.getWidth());
        Graphics2D g2d = (Graphics2D) mBufferStrategy.getDrawGraphics();
        Image img = createImage(MIS);
        /*g2d.drawImage(i
                mg, 0, 0, null);*/
        mBufferStrategy.show();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        ColorChooserDialog = new javax.swing.JDialog();
        ColorChooserWindow = new javax.swing.JColorChooser();
        SelectColorButton = new javax.swing.JButton();
        FileChooserDialog = new javax.swing.JDialog();
        FileChooserWindow = new javax.swing.JFileChooser();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        ColorButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        ColorPanel = new javax.swing.JPanel();
        ClearButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        StopButton = new javax.swing.JButton();
        ToBeginningButton = new javax.swing.JButton();
        RewindButton = new javax.swing.JButton();
        FastForwardButton = new javax.swing.JButton();
        ToEndButton = new javax.swing.JButton();
        PlayPauseButton = new javax.swing.JToggleButton();
        SelectFileButton = new javax.swing.JButton();
        FileNameLabel = new javax.swing.JLabel();
        LoopButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        PollingCheckBox = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        PublishButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        PollingRateComboBox = new javax.swing.JComboBox();
        GraphicsCanvas = new java.awt.Canvas();

        ColorChooserDialog.setTitle("Color Chooser");
        ColorChooserDialog.setMinimumSize(new java.awt.Dimension(480, 450));
        ColorChooserDialog.setName("Color Chooser Dialog"); // NOI18N
        ColorChooserDialog.setResizable(false);

        ColorChooserWindow.setName("ColorChooserWindow"); // NOI18N
        ColorChooserWindow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ColorChooserWindowMousePressed(evt);
            }
        });
        ColorChooserWindow.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                ColorChooserWindowPropertyChange(evt);
            }
        });

        SelectColorButton.setText("Select");
        SelectColorButton.setName("SelectColorButton"); // NOI18N
        SelectColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectColorButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ColorChooserDialogLayout = new javax.swing.GroupLayout(ColorChooserDialog.getContentPane());
        ColorChooserDialog.getContentPane().setLayout(ColorChooserDialogLayout);
        ColorChooserDialogLayout.setHorizontalGroup(
            ColorChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ColorChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap(378, Short.MAX_VALUE)
                .addComponent(SelectColorButton)
                .addContainerGap())
        );
        ColorChooserDialogLayout.setVerticalGroup(
            ColorChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ColorChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ColorChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(SelectColorButton)
                .addContainerGap())
        );

        FileChooserDialog.setMinimumSize(new java.awt.Dimension(602, 419));
        FileChooserDialog.setName("FileChooserDialog"); // NOI18N
        FileChooserDialog.setResizable(false);

        FileChooserWindow.setName("FileChooserWindow"); // NOI18N

        javax.swing.GroupLayout FileChooserDialogLayout = new javax.swing.GroupLayout(FileChooserDialog.getContentPane());
        FileChooserDialog.getContentPane().setLayout(FileChooserDialogLayout);
        FileChooserDialogLayout.setHorizontalGroup(
            FileChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FileChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FileChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        FileChooserDialogLayout.setVerticalGroup(
            FileChooserDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FileChooserDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(FileChooserWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/cospandesign/resources/cospandesign.png")));
        setResizable(false);

        jTabbedPane1.setName("ControlTabPanel"); // NOI18N

        jPanel2.setName("PaintPanel"); // NOI18N

        ColorButton.setText("Color Chooser");
        ColorButton.setName("ColorButton"); // NOI18N
        ColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ColorButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Paint Size");
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "5", "10", "20" }));
        jComboBox1.setSelectedIndex(2);
        jComboBox1.setName("jComboBox1"); // NOI18N

        ColorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        ColorPanel.setName("ColorPanel"); // NOI18N

        javax.swing.GroupLayout ColorPanelLayout = new javax.swing.GroupLayout(ColorPanel);
        ColorPanel.setLayout(ColorPanelLayout);
        ColorPanelLayout.setHorizontalGroup(
            ColorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 29, Short.MAX_VALUE)
        );
        ColorPanelLayout.setVerticalGroup(
            ColorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        ClearButton.setText("Clear");
        ClearButton.setName("ClearButton"); // NOI18N
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ColorButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(ColorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(394, 394, 394)
                        .addComponent(ClearButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(ColorPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ColorButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(ClearButton))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Paint", jPanel2);

        jPanel3.setName("MediaPanel"); // NOI18N

        StopButton.setText("Stop");
        StopButton.setEnabled(false);
        StopButton.setName("StopButton"); // NOI18N
        StopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StopButtonActionPerformed(evt);
            }
        });

        ToBeginningButton.setText("|<");
        ToBeginningButton.setEnabled(false);
        ToBeginningButton.setName("ToBeginningButton"); // NOI18N
        ToBeginningButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ToBeginningButtonActionPerformed(evt);
            }
        });

        RewindButton.setText("<<");
        RewindButton.setEnabled(false);
        RewindButton.setName("RewindButton"); // NOI18N
        RewindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RewindButtonActionPerformed(evt);
            }
        });

        FastForwardButton.setText(">>");
        FastForwardButton.setEnabled(false);
        FastForwardButton.setName("FastForwardButton"); // NOI18N
        FastForwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FastForwardButtonActionPerformed(evt);
            }
        });

        ToEndButton.setText(">|");
        ToEndButton.setEnabled(false);
        ToEndButton.setName("ToEndButton"); // NOI18N
        ToEndButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ToEndButtonActionPerformed(evt);
            }
        });

        PlayPauseButton.setText(">");
        PlayPauseButton.setEnabled(false);
        PlayPauseButton.setMultiClickThreshhold(1L);
        PlayPauseButton.setName("PlayPauseButton"); // NOI18N
        PlayPauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayPauseButtonActionPerformed(evt);
            }
        });

        SelectFileButton.setText("Select File");
        SelectFileButton.setName("SelectFileButton"); // NOI18N
        SelectFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SelectFileButtonActionPerformed(evt);
            }
        });

        FileNameLabel.setText("No File Selected");
        FileNameLabel.setName("FileNameLabel"); // NOI18N

        LoopButton.setText("Loop");
        LoopButton.setEnabled(false);
        LoopButton.setName("LoopButton"); // NOI18N
        LoopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoopButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(StopButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ToBeginningButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RewindButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PlayPauseButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(FastForwardButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ToEndButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LoopButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(SelectFileButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(FileNameLabel)))
                .addContainerGap(250, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ToBeginningButton)
                    .addComponent(RewindButton)
                    .addComponent(FastForwardButton)
                    .addComponent(ToEndButton)
                    .addComponent(PlayPauseButton)
                    .addComponent(StopButton)
                    .addComponent(LoopButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SelectFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(FileNameLabel))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Media", jPanel3);

        jPanel4.setName("jPanel4"); // NOI18N

        PollingCheckBox.setSelected(true);
        PollingCheckBox.setText("Polling");
        PollingCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        PollingCheckBox.setName("PollingCheckBox"); // NOI18N

        jCheckBox2.setText("Only on Change");
        jCheckBox2.setToolTipText("If new Image is different than the previous then a publish is sent");
        jCheckBox2.setEnabled(false);
        jCheckBox2.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jCheckBox2.setName("jCheckBox2"); // NOI18N

        PublishButton.setText("Publish");
        PublishButton.setName("PublishButton"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, PollingCheckBox, org.jdesktop.beansbinding.ELProperty.create("!${selected}"), PublishButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("Polling Rate");
        jLabel2.setName("jLabel2"); // NOI18N

        PollingRateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "10", "16", "20", "33", "50", "100", "1000", "10000" }));
        PollingRateComboBox.setSelectedIndex(3);
        PollingRateComboBox.setName("PollingRateComboBox"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PublishButton)
                .addGap(373, 373, 373)
                .addComponent(jCheckBox2)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(PollingCheckBox))
                    .addComponent(PollingRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(PollingCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PollingRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PublishButton)
                    .addComponent(jCheckBox2))
                .addGap(14, 14, 14))
        );

        jTabbedPane1.addTab("Publish", jPanel4);

        GraphicsCanvas.setName("GraphicsCanvas"); // NOI18N
        GraphicsCanvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                GraphicsCanvasMouseClicked(evt);
            }
        });
        GraphicsCanvas.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                GraphicsCanvasComponentResized(evt);
            }
        });
        GraphicsCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                GraphicsCanvasMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(49, Short.MAX_VALUE)
                        .addComponent(GraphicsCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(GraphicsCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("ControlTabPanel");

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PlayPauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayPauseButtonActionPerformed
        if (this.PlayPauseButton.isSelected()){
            this.PlayPauseButton.setText("||");
        }
        else{
            this.PlayPauseButton.setText(">");
        }
    }//GEN-LAST:event_PlayPauseButtonActionPerformed

    private void SelectFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectFileButtonActionPerformed
        if (FileChooserDialog.isVisible()){
            this.FileChooserDialog.setVisible(false);
        }
        else {
            this.FileChooserDialog.setVisible(true);
        }
    }//GEN-LAST:event_SelectFileButtonActionPerformed

    private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_StopButtonActionPerformed

    private void ToBeginningButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ToBeginningButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ToBeginningButtonActionPerformed

    private void RewindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RewindButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_RewindButtonActionPerformed

    private void FastForwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FastForwardButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FastForwardButtonActionPerformed

    private void ToEndButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ToEndButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ToEndButtonActionPerformed

    private void LoopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoopButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_LoopButtonActionPerformed

    private void ColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColorButtonActionPerformed
        if (this.ColorChooserDialog.isVisible()){
            this.ColorChooserDialog.setVisible(false);
        }
        else {
            this.ColorChooserDialog.setVisible(true);
        }
    }//GEN-LAST:event_ColorButtonActionPerformed

    private void ColorChooserWindowMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ColorChooserWindowMousePressed

    }//GEN-LAST:event_ColorChooserWindowMousePressed

    private void ColorChooserWindowPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_ColorChooserWindowPropertyChange

    }//GEN-LAST:event_ColorChooserWindowPropertyChange

    private void SelectColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SelectColorButtonActionPerformed
        SelectedColor = this.ColorChooserWindow.getColor();
        ColorPanel.setBackground(SelectedColor);
    }//GEN-LAST:event_SelectColorButtonActionPerformed

    private void GraphicsCanvasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GraphicsCanvasMouseClicked
       //mBufferedImage.setRGB(evt.getX(), evt.getY(), SelectedColor.getRGB());
       RGB[evt.getY() * GraphicsCanvas.getWidth() + evt.getX()] = SelectedColor.getRGB();
       MIS.newPixels();

       Graphics2D g2d = (Graphics2D) mBufferStrategy.getDrawGraphics();
       g2d.drawRenderedImage(mBufferedImage, null);
       mBufferStrategy.show();
    }//GEN-LAST:event_GraphicsCanvasMouseClicked

    private void GraphicsCanvasMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GraphicsCanvasMouseDragged
        GraphicsCanvasMouseClicked(evt);
    }//GEN-LAST:event_GraphicsCanvasMouseDragged

    private void GraphicsCanvasComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_GraphicsCanvasComponentResized
        SetupCanvas();
        mBufferStrategy.show();
    }//GEN-LAST:event_GraphicsCanvasComponentResized

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        SetupCanvas();
        mBufferStrategy.show();
    }//GEN-LAST:event_ClearButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GraphicsWidget().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton ColorButton;
    private javax.swing.JDialog ColorChooserDialog;
    private javax.swing.JColorChooser ColorChooserWindow;
    private javax.swing.JPanel ColorPanel;
    private javax.swing.JButton FastForwardButton;
    private javax.swing.JDialog FileChooserDialog;
    private javax.swing.JFileChooser FileChooserWindow;
    private javax.swing.JLabel FileNameLabel;
    private java.awt.Canvas GraphicsCanvas;
    private javax.swing.JButton LoopButton;
    private javax.swing.JToggleButton PlayPauseButton;
    private javax.swing.JCheckBox PollingCheckBox;
    private javax.swing.JComboBox PollingRateComboBox;
    private javax.swing.JButton PublishButton;
    private javax.swing.JButton RewindButton;
    private javax.swing.JButton SelectColorButton;
    private javax.swing.JButton SelectFileButton;
    private javax.swing.JButton StopButton;
    private javax.swing.JButton ToBeginningButton;
    private javax.swing.JButton ToEndButton;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables



}
